# -*- coding: utf-8 -*-
"""
2024-02-12 
main
"""
"""


from PySide6.QtWidgets import QApplication, QWidget

app = QApplication([])

# 创建一个QWidget实例
widget = QWidget()

# 设置窗口标题
widget.setWindowTitle('My First QWidget')

# 设置窗口大小
widget.setGeometry(100, 100, 300, 200)

# 显示窗口
widget.show()

# 运行应用程序的主事件循环
"""
from PySide6.QtCore import Slot, QSize
from PySide6.QtGui import QAction, QKeySequence
from PySide6.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QWidget
from PySide6.QtCore import Qt
from PySide6.QtGui import QGuiApplication, QCloseEvent
## from PySide6.QtCore.Qt import 

##from PySide6.QtCore import WindowState

from top.wardyan.utils import winformutils






class WMainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        ## self.setWindowTitle("Eartquakes information")
        
        ## 读取配置
        tite : str = winformutils.getStr(__name__, winformutils.DEFAULTSECTOR, "title", "开发辅助工具")
        fullscreen : bool = winformutils.getBool(__name__, winformutils.DEFAULTSECTOR, "fullscreen", False)
        width : int = winformutils.getInt(__name__, winformutils.DEFAULTSECTOR, "width", 800)
        height : int = winformutils.getInt(__name__, winformutils.DEFAULTSECTOR, "height", 600)
        x : int = winformutils.getInt(__name__, winformutils.DEFAULTSECTOR, "x", 0)
        y : int = winformutils.getInt(__name__, winformutils.DEFAULTSECTOR, "y", 0)
        ## winformutils.getInt(__name__, winformutils.DEFAULTSECTOR, "whatsthis", 0)
        position : int = winformutils.getInt(__name__, winformutils.DEFAULTSECTOR, "position", 0)

        ## 设置
        self.setWindowTitle(tite)
        if position == 0 :
            ## 如果居中窗口
            # geometry = self.screen().availableGeometry()
            # self.screen().availableSize()
            screen = QGuiApplication.primaryScreen().availableGeometry()
            ## size = self.geometry()
            x = (screen.width() - width) / 2
            y = (screen.height() - height) / 2

        self.setGeometry(x, y, width, height)
        ## 下面这个不能要, 会导致最大化按钮没了
        ## geometry = self.screen().availableGeometry()
        ## self.setMaximumSize(geometry.width(), geometry.height())
        if fullscreen :
            ## self.setWindowState(Qt.WindowState.WindowFullScreen)
            self.showMaximized()
        
        ## 显示窗体具体信息
        self.main()


        # hb : QHBoxLayout = QHBoxLayout()
        # self.setLayout(None)
        ## hb = self.layout()
        ##
        ## hb.setAlignment(Qt.AlignmentFlag.AlignLeft)
        # b = QPushButton("测试")
        # hb.addWidget(b)
        # self.setLayout(hb)
        # self.layout()
        ## self.layout().addChildWidget(hb)
        ## self.layout().addWidget(hb)
        ## self.layout()        
    ## @overload

    def closeEvent(self, event: QCloseEvent) -> None:
        """关闭窗体时记录窗体信息

        Args:
            event (QCloseEvent): _description_

        Returns:
            _type_: _description_
        """
        rs = super().closeEvent(event)
        g = self.geometry()
        ## QGuiApplication.screens()
        x = g.x()
        y = g.y()
        width = g.width()
        height = g.height()
        fullscreen = self.isMaximized()
        ## winformutils.setInt(__name__, winformutils.DEFAULTSECTOR, "width", w)
        cfg = winformutils.__get_cfg_file__(__name__)
        if not fullscreen :
            ## 最大化不改变原来所在的位置和大小
            cfg.setInt(winformutils.DEFAULTSECTOR, "x", x)
            cfg.setInt(winformutils.DEFAULTSECTOR, "y", y)
            cfg.setInt(winformutils.DEFAULTSECTOR, "width", width)
            cfg.setInt(winformutils.DEFAULTSECTOR, "height", height)
        cfg.setBool(winformutils.DEFAULTSECTOR, "fullscreen", fullscreen)
        winformutils.__save_cfg_file__(file=cfg)
        return rs
    # def resizeEvent(self, event: QResizeEvent) -> None:
    #     """
    #     改变窗体大小时记录
    #     """
    #     rs = super().resizeEvent(event)
    #     ## QSize old = event.oldSize()
    #     size : QSize = event.size()
    #     print(size)
    #     w = size.width()
    #     h = size.height()
    #     winformutils.setInt(__name__, winformutils.DEFAULTSECTOR, "width", w)
    #     winformutils.setInt(__name__, winformutils.DEFAULTSECTOR, "height", h)
    #     return rs

        """
        # Menu
        self.menu = self.menuBar()
        self.file_menu = self.menu.addMenu("File")

        # Exit QAction
        exit_action = QAction("Exit", self)
        exit_action.setShortcut(QKeySequence.Quit)
        exit_action.triggered.connect(self.close)

        self.file_menu.addAction(exit_action)

        # Status Bar
        self.status = self.statusBar()
        self.status.showMessage("Data loaded and plotted")

        # Window dimensions
        geometry = self.screen().availableGeometry()
        self.setFixedSize(geometry.width() * 0.8, geometry.height() * 0.7)
        
        geometry = self.screen().availableGeometry()
        self.setFixedSize(geometry.width() *0.8, geometry.height()  * 0.8)
        ##self.setWindowFlags(Qt.WindowState.WindowMaximized | Qt.WindowState.WindowMinimized)
        print(geometry)
        """

    def main(self) :
        HEIGHT = 50

        hb : QHBoxLayout = QHBoxLayout()
        
        ## 快捷菜单: 1脚本管理 2按序生成字符串 科启奥RFID模拟器 PDA开发辅助工具 二维码工具 自定义报表管理 预警管理 款式库
        buildstr = QPushButton("脚本管理")
        # buildstr.setsize
        buildstr.setMinimumHeight(HEIGHT)
        hb.addWidget(buildstr)
        
        buildstr = QPushButton("按序生成字符串")
        buildstr.setMinimumHeight(HEIGHT)
        hb.addWidget(buildstr)
        
        buildstr = QPushButton("科启奥RFID模拟器")
        buildstr.setMinimumHeight(HEIGHT)
        hb.addWidget(buildstr)
        
        buildstr = QPushButton("PDA开发辅助工具")
        buildstr.setMinimumHeight(HEIGHT)
        hb.addWidget(buildstr)
        
        buildstr = QPushButton("二维码工具")
        buildstr.setMinimumHeight(HEIGHT)
        hb.addWidget(buildstr)
        
        buildstr = QPushButton("自定义报表管理")
        buildstr.setMinimumHeight(HEIGHT)
        hb.addWidget(buildstr)
        
        buildstr = QPushButton("预警管理")
        buildstr.setMinimumHeight(HEIGHT)
        hb.addWidget(buildstr)

        buildstr = QPushButton("款式库")
        buildstr.setMinimumHeight(HEIGHT)
        hb.addWidget(buildstr)
        
        menuBar = QWidget()
        menuBar.setLayout(hb)
        menuBar.setMinimumHeight(HEIGHT)
        self.setMenuWidget(menuBar)
        
if __name__ == "__main__" or 1 == 1:
    app = QApplication([])
    w = WMainWindow()
    w.show()
    app.exec()